package com.youxin.logprocess.mysql;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author yingde.cao
 *
 */
public class MysqlDbHelper {
    public static MysqlDbHelper instance = null;    
    private String nombrebd = null;   
    private String usuario = null;   
    private String passwd = null;    
    private String host = null;    
    private boolean configured = false;
    private Connection conexion = null;
    private String url = null;    
    private MysqlDbHelper() {    	
    }
    
    public static MysqlDbHelper getInstance() {
        if (instance == null) {
            instance = new MysqlDbHelper();
        }
        return instance;
    }
    
    public static MysqlDbHelper configure(String url, String user, String passwd) {
    	MysqlDbHelper md = getInstance();
        if ( user == null || user.isEmpty() || passwd == null) {
            throw new IllegalArgumentException("参数为空");
        }
       // md.host = host;
        md.passwd = passwd;
        md.usuario = user;
       // md.nombrebd = bd;
        md.url = url;
        md.configured = true;
        return md;
    }

    private boolean establecerConexion() {
        boolean flag = false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexion = (Connection) DriverManager.getConnection(url, usuario, passwd);
            flag = true;
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        }
        return flag;
    }

    private void cerrarConexion() {
        try {
            if (conexion != null && !conexion.isClosed())
                conexion.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public boolean consultaSimple(String query) {
        if (!this.configured) {
            throw new IllegalStateException("ModeloDB no configurado!");
        }
        boolean flag = false;
        if (this.establecerConexion()) {            
            try (Statement consulta = (Statement) conexion.createStatement()) {
                if (consulta != null) {
                    consulta.executeUpdate(query);
                    flag = true;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                cerrarConexion();
            }
        }
        return flag;
    }

    public List<Map<String,String>> consultaConResultado(String query) {
        if (!this.configured) {
            throw new IllegalStateException("配置失败");
        }
        List<Map<String,String>> fila = null;
        if (this.establecerConexion()) {            
            try (Statement consulta = (Statement) conexion.createStatement()) {
                if (consulta != null) {
                    ResultSet rs = consulta.executeQuery(query);
                    ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
                    int columnas = meta.getColumnCount();
                    fila = new ArrayList<>();
                    while (rs.next()) {
                        Map<String, String> columna = new HashMap<>();
                        for (int i = 1; i <= columnas; i++) {
                            String clave = meta.getColumnName(i);
                            String valor = rs.getString(i);
                            columna.put(clave, valor);
                        }
                        fila.add(columna);
                    }

                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                cerrarConexion();
            }
        }
        return fila;
    }

}